package com.tjbank.cssys.service.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.tjbank.cssys.framework.base.enums.YesOrNoEnum;
import com.tjbank.cssys.dto.PermissionDTO;

public class PermissionUtil {

    /**
     * listToTree
     * 
     * @param permissionList
     * @return
     * @author -- lijiangtao 2020年7月24日 上午10:21:32
     */
    public static List<PermissionDTO> listToTree(List<PermissionDTO> permissionList) {
        List<PermissionDTO> treeList = new ArrayList<PermissionDTO>();
        for (PermissionDTO permission : permissionList) {
            if (Objects.equals(permission.getMenuId(), YesOrNoEnum.NO.getCode())) {
                treeList.add(findChildren(permission, permissionList));
            }
        }
        return treeList;
    }

    /**
     * 递归
     * 
     * @param menu
     * @param menuList
     * @return
     * @author -- lijiangtao 2020年7月14日 下午12:47:32
     */
    private static PermissionDTO findChildren(PermissionDTO parentPermiss, List<PermissionDTO> permissionList) {
        for (PermissionDTO node : permissionList) {
            if (Objects.equals(parentPermiss.getId(), node.getMenuId())) {
                if (Objects.isNull(parentPermiss.getChildren())) {
                    parentPermiss.setChildren(new ArrayList<>());
                }
                parentPermiss.getChildren().add(findChildren(node, permissionList));
            }
        }
        return parentPermiss;
    }
}
